package com.service;


import com.config.MQConfig;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class MQService {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    /**
     * @param exchange 发送到哪一个交换机
     * @param msg  发送内容
     * @param key  发送到哪一个队列
     */
    public void sendMessage(String exchange,String key,String msg){

        rabbitTemplate.convertAndSend(exchange,key,msg);
    }

    @RabbitListener(queues = MQConfig.QUEUE_MIAOSHA)
    @RabbitHandler
    public void reciveMiaosha(String msg){
        System.out.println("收到秒杀消息："+msg);
    }


    @RabbitListener(queues = MQConfig.QUEUE_SENDMESSAGE)
    @RabbitHandler
    public void recivMessage(String msg){
        System.out.println("收到聊天消息："+msg);
    }

}
